﻿using System;
using System.Collections.Generic;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Foundation.Collections;
using XiaoweiV2;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class PCControlResponseCreator
    {
        private class PCControlResponse : IPCControlResponseData
        {
            public PCControlType Type { get; set; }

            public double Value { get; set; }

            public VoiceAI.ResponseType ResponseType => VoiceAI.ResponseType.PCControl;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }

            public string ResponseText { get; set; }

            public string TTSUrl { get; set; }

            public bool RestoreInHistory => true;

            public object DataContext { get; set; }
        }

        private class DefaultResponse : IResponseData
        {
            public VoiceAI.ResponseType ResponseType => VoiceAI.ResponseType.Default;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }

            public string ResponseText { get; set; }

            public string TTSUrl { get; set; }

            public bool RestoreInHistory => true;

            public object DataContext { get; set; }
        }
        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, ResponseParam param)
        {
            if(!double.TryParse(param.ControlValue,out double value))
            {
                value = -300;
            }
            if(!commandIdAndControlTypeMap.ContainsKey(param.ControlId))
            {
                return new DefaultResponse() {
                    RequestText= param.RequestText,
                    ResponseText= "对不起，小微无法理解您的意思。",
                    TTSUrl= xiaoweiVoiceAI.TextToVoice("对不起，小微无法理解您的意思。")
            };
            }
            PCControlResponse pCControlResponse = new PCControlResponse();
            pCControlResponse.Type = commandIdAndControlTypeMap[param.ControlId];
            pCControlResponse.RequestText = param.RequestText;

            if (!string.IsNullOrEmpty(param.ResponseText))
            {
                pCControlResponse.ResponseText = param.ResponseText;
                pCControlResponse.TTSUrl = ResponseHelper.GetTTSUrl(param);
            }
            else
            {
                pCControlResponse.ResponseText = defaultResponseTxt[param.ControlId];
                pCControlResponse.TTSUrl = xiaoweiVoiceAI.TextToVoice(defaultResponseTxt[param.ControlId]);
            }
            pCControlResponse.Value = value/100;
            return pCControlResponse;

        }
        private readonly static IReadOnlyDictionary<CommandId, string> defaultResponseTxt = new Dictionary<CommandId, string>()
        {
            {CommandId.AddVolume,DefaultResponseTxts.SetVolumeOk },
            {CommandId.SetVolumeTo,DefaultResponseTxts.SetVolumeOk },
            {CommandId.MuteSpeaker,DefaultResponseTxts.Ok },
            {CommandId.UnmuteSpeaker,DefaultResponseTxts.Ok },
            {CommandId.AddBrightness,DefaultResponseTxts.SetBrightnessOk },
            {CommandId.SetBrightnessTo,DefaultResponseTxts.SetBrightnessOk },
            {CommandId.Shutdown,DefaultResponseTxts.Ok },
        };

        private readonly static IReadOnlyDictionary<CommandId, PCControlType> commandIdAndControlTypeMap = new Dictionary<CommandId, PCControlType>()
        {
            {CommandId.AddVolume,PCControlType.AddVolume },
            {CommandId.SetVolumeTo,PCControlType.SetVolumeTo },
            {CommandId.MuteSpeaker,PCControlType.MuteSpeaker },
            {CommandId.UnmuteSpeaker,PCControlType.UnmuteSpeaker },
            {CommandId.AddBrightness,PCControlType.AddBrightness },
            {CommandId.SetBrightnessTo,PCControlType.SetBrightnessTo },
            {CommandId.Shutdown,PCControlType.Shutdown },
        };

        private static class DefaultResponseTxts
        {
            public const string Ok = "好的";
            public const string SetBrightnessOk = "好的，设置亮度成功";
            public const string SetVolumeOk = "好的，设置音量成功";
        }
    }
}
